package com.jala.demo.api;

import com.jala.cm.BizCode;
import com.jala.cm.BizException;
import com.jala.cm.PageResult;
import com.jala.demo.model.Demo;
import com.jala.demo.model.DemoVO;
import com.jala.demo.req.*;
import com.jala.demo.resp.*;
import com.jala.demo.service.DemoService;
import org.dozer.DozerBeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/15.
 */
@Service
public class DemoApiServiceImpl implements DemoApiService{
    private final Logger logger= LoggerFactory.getLogger(DemoApiServiceImpl.class);

    @Autowired
    private DemoService demoService;

    @Override
    public PageDemoResp pageDemoList(PageDemoReq req) {
        logger.info("pageDemoList：{}", req);
        PageDemoResp resp = new PageDemoResp();
        if(req==null){
            logger.warn("pageDemoList...请求参数为null！");
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        try {
            req.check();//请求数据校验
        } catch (BizException e) {
            logger.error(e.getMessage(), e);
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        PageResult<Demo> demoList = demoService.pageDemoList(req);
        List<Demo> demos = demoList.getResults();
        List<DemoVO> demoVOS = new ArrayList<DemoVO>();
        DozerBeanMapper mapper = new DozerBeanMapper();
        if(demos!=null && demos.size()>0){
            for (int i = 0; i < demos.size(); i++) {
                DemoVO demoVO = mapper.map(demos.get(i), DemoVO.class);
                demoVOS.add(demoVO);
            }
        }
        resp.setTotalCount(demoList.getTotalResults());
        resp.setDemoVOS(demoVOS);
        return resp;
    }

    @Override
    public AddDemoResp addDemo(AddDemoReq req) {
        logger.info("addDemo：{}", req);
        AddDemoResp resp = new AddDemoResp();
        if(req==null){
            logger.warn("addDemo...请求参数为null！");
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        try {
            req.check();//请求数据校验
        } catch (BizException e) {
            logger.error(e.getMessage(), e);
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        DozerBeanMapper mapper = new DozerBeanMapper();
        Demo demo = mapper.map(req.getDemoVO(), Demo.class);
        int i = demoService.addDemo(demo);
        if(i<=0){
            resp.setCode(-1);
            resp.setMsg("操作失败！");
        }
        return resp;
    }

    @Override
    public EditDemoResp editDemo(EditDemoReq req) {
        logger.info("editDemo：{}", req);
        EditDemoResp resp = new EditDemoResp();
        if(req==null){
            logger.warn("editDemo...请求参数为null！");
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        try {
            req.check();//请求数据校验
        } catch (BizException e) {
            logger.error(e.getMessage(), e);
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        DozerBeanMapper mapper = new DozerBeanMapper();
        Demo demo = mapper.map(req.getDemoVO(), Demo.class);
        int i = demoService.editDemo(demo);
        if(i<=0){
            resp.setCode(-1);
            resp.setMsg("操作失败！");
        }
        return resp;
    }

    @Override
    public DelDemoResp deleteDemo(DelDemoReq req) {
        logger.info("deleteDemo：{}", req);
        DelDemoResp resp = new DelDemoResp();
        if(req==null){
            logger.warn("deleteDemo...请求参数为null！");
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        try {
            req.check();//请求数据校验
        } catch (BizException e) {
            logger.error(e.getMessage(), e);
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        int i = demoService.deleteDemo(req.getId());
        if(i<=0){
            resp.setCode(-1);
            resp.setMsg("操作失败！");
        }
        return resp;
    }

    @Override
    public ListDemoResp listDemo(ListDemoReq req) {
        logger.info("listDemo：{}", req);
        ListDemoResp resp = new ListDemoResp();
        if(req==null){
            logger.warn("listDemo...请求参数为null！");
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        try {
            req.check();//请求数据校验
        } catch (BizException e) {
            logger.error(e.getMessage(), e);
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        List<Demo> demos = demoService.listDemo(req.getId());
        List<DemoVO> demoVOS = new ArrayList<DemoVO>();
        DozerBeanMapper mapper = new DozerBeanMapper();
        if(demos!=null && demos.size()>0){
            for (int i = 0; i < demos.size(); i++) {
                DemoVO demoVO = mapper.map(demos.get(i), DemoVO.class);
                demoVOS.add(demoVO);
            }
        }
        resp.setDemoVOS(demoVOS);
        return resp;
    }

    @Override
    public GetDemoResp getDemo(GetDemoReq req) {
        logger.info("getDemo：{}", req);
        GetDemoResp resp = new GetDemoResp();
        if(req==null){
            logger.warn("getDemo...请求参数为null！");
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        try {
            req.check();//请求数据校验
        } catch (BizException e) {
            logger.error(e.getMessage(), e);
            resp.setCode(-1);
            resp.setMsg(BizCode.C10001.getMessage());
            return resp;
        }
        Demo demo = demoService.queryByPrimaryKey(req.getId());
        DozerBeanMapper mapper = new DozerBeanMapper();
        DemoVO demoVO = mapper.map(demo, DemoVO.class);
        resp.setDemoVO(demoVO);
        return resp;
    }
}
